#!/bin/bash

set -eo pipefail

if [[ $# -ne 3 ]]; then
  echo "usage: $0 <repository> <tag|tag_with_sha> <match>"
  exit 1
fi

case "$2" in
  tag) FILTER='.tag_name' ;;
  tag_with_sha) FILTER='(.tag_name + "-g" + (.target_commitish | .[0:12]))' ;;
  *) echo "Invalid: $2"; exit 1 ;;
esac

curl --fail -s "https://api.github.com/repos/ayufan-rock64/$1/releases" | \
  jq -r ".[] | $FILTER" | \
  grep -m 1 "^$3-"
